<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>注册信息</title>
</head>
<body>
<?php
if (isset ( $_POST ['user_image_button'] ) and $_POST ['user_image_button'] == "上传") {
	$user_image = $_FILES ["user_image"] ["tmp_name"] . $_FILES ["user_image"] ["name"];
	echo '<pre>' . print_r ( $_FILES ["user_image"] ) . '</pre>';
	if ((($_FILES ["user_image"] ["type"] == "image/gif") || ($_FILES ["user_image"] ["type"] == "image/jpeg") || ($_FILES ["user_image"] ["type"] == "image/jpg")) && ($_FILES ["user_image"] ["size"] < 4000000)) {
		if ($_FILES ["user_image"] ["error"] > 0) {
			echo "出现错误，错误编码为: " . $_FILES ["user_image"] ["error"] . "<br />";
		} else {
			if (file_exists ( "uploadImages/" . $_FILES ["user_image"] ["name"] )) {
				// echo $_FILES ["user_image"] ["name"] . " 已经存在了. ";
				header ( "location:register.php?user_img=-1" );
			} else {
				move_uploaded_file ( $_FILES ["user_image"] ["tmp_name"], "uploadImages/" . $_FILES ["user_image"] ["name"] );
				// 文件信息
				/*
				 * echo "Stored in: " . "uploadImages/" . $_FILES ["file"] ["name"]; echo "Upload: " . $_FILES ["user_image"] ["name"] . "<br />"; echo "Type: " . $_FILES ["user_image"] ["type"] . "<br />"; echo "Size: " . ($_FILES ["user_image"] ["size"] / 1024) . "Kb <br />"; echo "Temp file: " . $_FILES ["user_image"] ["tmp_name"] . "<br />"; echo "<img src='" . $user_image . "'/>";
				 */
				$user_image = "uploadImages/" . $_FILES ["user_image"] ["name"];
				header ( "location:register.php?user_img=$user_image" );
			}
		}
	} else {
		// echo "无效的文件";
		header ( "location:register.php?user_img=-2" );
	}
}
if (isset ( $_POST ['user_submit'] ) and $_POST ['user_submit'] == "立即注册") {
	$user_account = $_POST ['user_account'];
	include '../includes/dbhelper.php';
	$result = mysql_query ( "select account from `bookmgr_user_info` where `account` ='$user_account'" );
	$row = mysql_num_rows ( $result );
	if ($row > 0) {
		// 没有查找到表示登录失败
		echo "该用户已存在，请重新填写：&nbsp;<a href='../login.php'>返回登录</a>";
		return;
	}
	$user_password = $_POST ['user_password'];
	$user_name = $_POST ['user_name'];
	$user_image = $_POST ['user_image_path'];
	$is_secure = $_POST ['is_secure'];
	$user_question = $_POST ['user_question'];
	$user_answer = $_POST ['user_answer'];
	
	// （可选，一般用于登录时将用户信息放入）如果是当前用户的信息，则初始化session，将信息保存到会话中
	// session_start ();
	// $_SESSION ['userinfo'] = array (
	// "name" => $n,
	// "age" => $a
	// );
	// 将用户信息保存到数据库中
//	$conn = mysql_connect ( "localhost", "root", "111111" ) or die ( "数据库连接错误" );
	// $result = mysql_query ( "insert into user_info ( name,age )values('$account',$select)" );
//	mysql_select_db ( "bookmgr", $conn ) or die ( "数据库访问错误" );
//	mysql_query ( "set names utf8" );
	$result = mysql_query ( "insert into bookmgr_user_info ( username,account,password,is_secure,user_question,user_answer,user_images )
			values('$user_name','$user_account',md5('$user_password'),'$is_secure','$user_question','$user_answer','$user_image')" );
	if (! ! $result) {
		// echo "<script>alert('提交成功')</script>";
		echo "恭喜注册成功&nbsp;<a href='../login.php'>返回登录</a>";
	} else {
		// echo "<script>alert('提交失败')</script>";
		echo "注册失败,请检查网络后重新注册&nbsp;<a href='../login.php'>返回登录</a>";
	}
	mysql_close ( $conn );
}
?>
</body>
</html>
